package com.shr.codem;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Five {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		while (sc.hasNextInt()) {
			int count = sc.nextInt();
			int numberOfGroup = sc.nextInt();
			int[] a = new int[count];
			for (int j = 0; j < count; j++) {
				a[j] = sc.nextInt();
			}
			int[] results = new int[numberOfGroup];
			for (int i = 0; i < numberOfGroup; i++) {
				int startValue = sc.nextInt() - 1;
				int endValue = sc.nextInt();
				int testNum = sc.nextInt();
				Map<Integer, Integer> numMap = new HashMap<>();
				for (int k = startValue; k < endValue; k++) {
					Integer num = numMap.get(a[k]);
					if (num == null) {
						numMap.put(a[k], 1);
					} else
						numMap.put(a[k], num + 1);
				}
				int number = 0;
				for (int aKey : numMap.keySet()) {
					if (isZhi(numMap.get(aKey), testNum))
						number++;
				}
				results[i] = number;
			}
			for (int i = 0; i < results.length; i++)
				System.out.println(results[i]);
		}
		sc.close();
	}

	private static boolean isZhi(Integer integer, int testNum) {
		if ((int) integer == 1 || testNum == 1)
			return true;
		else {
			int aMin = Math.min(integer, testNum);
			for (int i = 2; i <= aMin; i++) {
				if (testNum % i == 0 && integer % i == 0)
					return false;
			}
			return true;
		}
	}
}
